﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security;

namespace sorochinska_kl.Models
{
    public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Assessment> Assessments { get; set; }
        public DbSet<AssessmentStatus> AssessmentStatuses { get; set; }
        public DbSet<DeliveryTask> DeliveryTasks { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<EmployeeBasicEducation> EmployeeBasicEducations { get; set; }
        public DbSet<EmployeePerfRating> EmployeePerfRatings { get; set; }
        public DbSet<EmployeeStatus> EmployeeStatuses { get; set; }
        public DbSet<GeneralStatus> GeneralStatuses { get; set; }
        public DbSet<Office> Offices { get; set; }
        public DbSet<Position> Positions { get; set; }
        public DbSet<Rating> Ratings { get; set; }
        public DbSet<Skill> Skills { get; set; }
        public DbSet<SkillDevPlan> SkillDevPlans { get; set; }
        public DbSet<SkillLevel> SkillLevels { get; set; }
        public DbSet<SkillType> SkillTypes { get; set; }
        public DbSet<UserSkill> UserSkills { get; set; }
        public DbSet<Role> UserRoles { get; set; }
        public DbSet<Training> Trainings { get; set; }
        public DbSet<UserTraining> UserTrainings { get; set; }
        

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Employee>()
                .HasMany<Skill>(s => s.Skills)
                .WithMany(u => u.Employees)
                .Map(m =>
                {
                    m.ToTable("UserSkill");
                    m.MapLeftKey("UserId");
                    m.MapRightKey("SkillID");
                });

            modelBuilder.Entity<Employee>()
                .HasMany<Training>(t => t.Trainings)
                .WithMany(e => e.Employees)
                .Map(m => {
                    m.ToTable("UserTrainings");
                    m.MapLeftKey("UserId");
                    m.MapRightKey("TrainingID");
                });


            modelBuilder.Entity<Membership>()
              .HasMany<Role>(r => r.Roles)
              .WithMany(u => u.Members)
              .Map(m =>
              {
                  m.ToTable("webpages_UsersInRoles");
                  m.MapLeftKey("UserId");
                  m.MapRightKey("RoleId");
              });
        }

    }

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }

        //public virtual ICollection<Role> Roles { get; set; }
    }

    public class RegisterExternalLoginModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        public string ExternalLoginData { get; set; }
    }

    public class LocalPasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class LoginModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class ExternalLogin
    {
        public string Provider { get; set; }
        public string ProviderDisplayName { get; set; }
        public string ProviderUserId { get; set; }
    }

    public class AddUserToRole
    {
        [Required]
        [Display(Name = "User name")]
        public int UserId { get; set; }

        [Required]
        [Display(Name = "Role")]
        public int RoleId { get; set; }

    }




    #region Added Code

    [Table("webpages_Membership")]
    public class Membership
    {
        public Membership()
        {
            Roles = new List<Role>();
            OAuthMemberships = new List<OAuthMembership>();
        }

        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int UserId { get; set; }
        public DateTime? CreateDate { get; set; }
        [StringLength(128)]
        public string ConfirmationToken { get; set; }
        public bool? IsConfirmed { get; set; }
        public DateTime? LastPasswordFailureDate { get; set; }
        public int PasswordFailuresSinceLastSuccess { get; set; }
        [Required, StringLength(128)]
        public string Password { get; set; }
        public DateTime? PasswordChangedDate { get; set; }
        [Required, StringLength(128)]
        public string PasswordSalt { get; set; }
        [StringLength(128)]
        public string PasswordVerificationToken { get; set; }
        public DateTime? PasswordVerificationTokenExpirationDate { get; set; }

        public ICollection<Role> Roles { get; set; }

        [ForeignKey("UserId")]
        public ICollection<OAuthMembership> OAuthMemberships { get; set; }
    }

    [Table("webpages_OAuthMembership")]
    public class OAuthMembership
    {
        [Key, Column(Order = 0), StringLength(30)]
        public string Provider { get; set; }

        [Key, Column(Order = 1), StringLength(100)]
        public string ProviderUserId { get; set; }

        public int UserId { get; set; }

        [Column("UserId"), InverseProperty("OAuthMemberships")]
        public Membership User { get; set; }
    }

    [Table("webpages_Roles")]
    public class Role
    {
        public Role()
        {
            Members = new List<Membership>();
        }

        [Key]
        public int RoleId { get; set; }
        [StringLength(256)]
        public string RoleName { get; set; }

        public ICollection<Membership> Members { get; set; }
        public virtual ICollection<UserProfile> UserProfiles { get; set; }
    }

    #endregion

    public class Employee
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int UserId { get; set; }
        public int? ManagerID { get; set; }
        public int? HRManagerID { get; set; }
        public int PositionID { get; set; }
        public int DepartmentID { get; set; }
        public int OfficeID { get; set; }
        public int EmployeeStatusID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Start date")]
        public DateTime StartDate { get; set; }

        [MaxLength(13, ErrorMessage="Phone number must be 13 characters of less"),MinLength(10)]
        [Display(Name = "Work phone")]
        public string WorkPhone { get; set; }
        [MaxLength(13, ErrorMessage = "Phone number must be 13 characters of less"), MinLength(10)]
        [Display(Name = "Cell phone")]
        public string CellPhone { get; set; }
        public string Email { get; set; }

        [Display(Name = "Full name")]
        public string FullName { get {
            return this.FirstName + " " + this.LastName;
        } }

        //public virtual UserProfile User { get; set; }
        public virtual Position Position { get; set; }
        public virtual Department Department { get; set; }
        public virtual Office Office { get; set; }
        public virtual EmployeeStatus EmployeeStatus { get; set; }
        //public virtual UserTraining UserTraining { get; set; }

        public virtual ICollection<Assessment> Assessments { get; set; }
        public virtual ICollection<EmployeeBasicEducation> EmployeeBasicEducations { get; set; }
        public virtual ICollection<Skill> Skills { get; set; }
        public virtual ICollection<Training> Trainings { get; set; }

        //public virtual Employee Manager { get; set; }
        //public virtual Employee HRManager { get; set; }

    }

    public class EmployeeBasicEducation
    {
        public int EmployeeBasicEducationID { get; set; }
        public int UserId { get; set; }
        [Display(Name = "Name of School/College/University etc.")]
        public string Name { get; set; }
        public string Department { get; set; }
        public string Specialty { get; set; }

        [Display(Name = "Diploma/Degree/Level of competition")]
        public string Degree { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Start Date")]
        public DateTime StartDate { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "End Date")]
        public DateTime EndDate { get; set; }

        public virtual Employee Employee { get; set; }
    }

    public class EmployeePerfRating
    {
        public int EmployeePerfRatingID { get; set; }
        [Display(Name = "Performance rating")]
        public string Name { get; set; }
    }

    public class EmployeeStatus
    {
        public int EmployeeStatusID { get; set; }
        [Display(Name = "Employee status")]
        public string Name { get; set; }
    }

    public class GeneralStatus
    {
        [Key]
        public int StatusID { get; set; }
        [Display(Name = "Status")]
        public string Name { get; set; }
    }

    public class Office
    {
        public int OfficeID { get; set; }
        [Display(Name = "Office name")]
        public string Name { get; set; }
        public string Country { get; set; }
        public string Region { get; set; }
        public string City { get; set; }
        public string Address { get; set; }
    }

    public class Position
    {
        public int PositionID { get; set; }
        [Display(Name = "Position")]
        public string Name { get; set; }
    }
    
    public class Rating
    {
        public int RatingID { get; set; }
        [Display(Name = "Rating")]
        public string Name { get; set; }
    }

    public class Skill
    {
        public int SkillID { get; set; }
        public int SkillTypeID { get; set; }
        [Display(Name="Skill title")]
        public string Name { get; set; }

        public virtual SkillType SkillType { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }
    }

    public class SkillDevPlan
    {
        public int SkillDevPlanID { get; set; }
        public int AssessmentID { get; set; }
        public int SkillLevelID { get; set; }
        [Display(Name="Development way")]
        public string DevWay { get; set; }
        [Display(Name = "Actions to take")]
        public string ActionsToTake { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Due date")]
        public DateTime DueDate { get; set; }

        public int StatusID { get; set; }
        [Display(Name = "Manager comments")]
        public string MngComments { get; set; }
        [Display(Name = "Approved by manager")]
        public bool ApprovedByMng { get; set; }


        public virtual Assessment Assessment { get; set; }
        public virtual SkillLevel SkillLevel { get; set; }
        public virtual GeneralStatus Status { get; set; }
    }

    public class SkillLevel
    {
        public int SkillLevelID { get; set; }
        [Display(Name = "Skill level title")]
        public string Name { get; set; }
    }

    public class SkillType
    {
        public int SkillTypeID { get; set; }
        [Display(Name = "Skill type title")]
        public string Name { get; set; }
    }

    public class UserSkill
    {
        [Key, Column(Order = 0)]
        public int UserId { get; set; }
        [Key, Column(Order = 1)]
        public int SkillID { get; set; }

        public int SkillLevelID { get; set; }

        public virtual Employee Employee { get; set; }
        public virtual Skill Skill { get; set; }
    }

    public class Assessment
    {
        public int AssessmentID { get; set; }
        public int UserId { get; set; }

        [Display(Name = "Assessment title")]
        public string Name { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Start date")]
        public DateTime StartDate { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "End date")]
        public DateTime EndDate { get; set; }

        [Display(Name = "Career Growth. Employee Expectation")]
        public string CareerGrowthEmpl { get; set; }
        [Display(Name = "Professional Growth. Employee Expectations")]
        public string ProfGrowthEmpl { get; set; }
        [Display(Name = "Career Growth. Manager Expectations")]
        public string CareerGrowthMng { get; set; }
        [Display(Name = "Professional Growth. Manager Expectations")]
        public string ProfGrowthMng { get; set; }

        public int AssesmentStatusID { get; set; }
        public int EmployeePerfRatingID { get; set; }


        public virtual Employee Employee { get; set; }
        public virtual AssessmentStatus AssessmentStatus { get; set; }
        public virtual EmployeePerfRating EmployeePerfRating { get; set; }

    }

    public class AssessmentStatus
    {
        public int AssessmentStatusID { get; set; }
        [Display(Name = "Assessment status")]
        public string Name { get; set; }
    }

    public class DeliveryTask
    {
        public int DeliveryTaskID { get; set; }
        public int AssessmentID { get; set; }
        [Display(Name = "Delivery task")]
        public string Name { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Due date")]
        public DateTime DueDate { get; set; }

        public string Plan { get; set; }
        public int StatusID { get; set; }
        [Display(Name = "Approved by manager")]
        public bool ApprovedByManager { get; set; }
        public int MngRatingID { get; set; }
        public int EmplRatingID { get; set; }

        public virtual Assessment Assessment { get; set; }
        public virtual GeneralStatus Status { get; set; }

        public virtual Rating MngRating { get; set; }
        public virtual Rating EmplRating { get; set; }

    }

    public class Department
    {
        public int DepartmentID { get; set; }
        [Display(Name = "Department")]
        public string Name { get; set; }
    }


    public class Training
    {
        public int TrainingID { get; set; }
        [Display(Name = "Training title")]
        public string Name { get; set; }
        public string Instructor { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Start date")]
        public DateTime StartDate { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "End date")]
        public DateTime EndDate { get; set; }
        [Display(Name = "Way of attendance")]
        public string WayOfAttendance { get; set; }

        //public virtual UserTraining UserTraining { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }
    }

    public class UserTraining
    {
        [Key, Column(Order = 0)]
        public int UserId { get; set; }
        [Key, Column(Order = 1)]
        public int TrainingID { get; set; }
        [Display(Name = "Test result")]
        public int? TestResult { get; set; }

       
 
    }

}

